package com.egao.common.publicly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.constants.AttachmentConstants;
import com.egao.common.module.company.entity.Attachment;
import com.egao.common.module.company.entity.Mutual;
import com.egao.common.module.company.entity.MutualApply;
import com.egao.common.module.company.entity.vo.MutualApplyFormVO;
import com.egao.common.module.company.entity.vo.MutualFormVO;
import com.egao.common.module.company.service.AttachmentService;
import com.egao.common.module.company.service.MutualApplyService;
import com.egao.common.publicly.entity.ApplyButtonVO;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import com.egao.common.publicly.entity.PubliclyMutualApplyVO;
import com.egao.common.publicly.entity.PubliclyMutualCompanyVO;
import com.egao.common.publicly.mapper.PubliclyMutualMapper;
import com.egao.common.publicly.service.PubliclyMutualService;
import com.egao.common.publicly.utils.CalendarUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 首页校内双选会接口实现类
 * @author pyx
 */
@Service
public class PubliclyMutualServiceImpl extends ServiceImpl<PubliclyMutualMapper, Mutual> implements PubliclyMutualService {
    @Autowired
    private MutualApplyService mutualApplyService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    public List<PubliclyCareerVO> listPage(PageParam<PubliclyCareerVO> pageParam) {
        CalendarUtils.putTime(pageParam);
        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult getId(Integer userId, Integer id) {
        Mutual mutual = baseMapper.selectById(id);
        if (mutual == null) {
            return JsonResult.error();
        }

        //双选会报名验证
        MutualFormVO mutualFormVO = new MutualFormVO();
        mutualFormVO.setId(id);
        mutualFormVO.setCreateBy(userId);

        JsonResult jsonResult;
        ApplyButtonVO applyButton = new ApplyButtonVO();
        ApplyButtonVO cancelButton = new ApplyButtonVO();
        cancelButton.setButtonText("取消");

        if(userId == null){
            applyButton.setButtonState(2);
            applyButton.setButtonText("未登录");
            cancelButton.setButtonState(2);
            jsonResult = JsonResult.ok().put("applyButton",applyButton).put("cancelButton",cancelButton);
        }else {
            jsonResult = mutualApplyService.applyMutual(mutualFormVO);
        }

        mutual.setHits(mutual.getHits() != null ? mutual.getHits() + 1 : 1);
        baseMapper.updateById(mutual);

        //获取双选会所有报名信息
        List<PubliclyMutualCompanyVO> listApply = baseMapper.listApplyPage(id);

        //获取统计数据
        PubliclyMutualApplyVO countApply = baseMapper.coutApply(id);
        if(countApply != null){
            applyNum(countApply);
        }
        if(listApply.size() > 0){
            countApply.setListCompany(listApply);
        }
        jsonResult.put("applyData",countApply);

        //插入附件集合
        List<Attachment> attachmentList = attachmentService.getAttachByBusinessId(mutual.getId(), AttachmentConstants.TYPE_MUTUAL);
        if (attachmentList.size() == 0) {
            mutual.setAttachmentList(null);
        } else {
            mutual.setAttachmentList(attachmentList);
        }

        return jsonResult.setData(mutual);
    }

    @Override
    public JsonResult getApply(Integer mutualId,Integer companyId) {
        MutualApplyFormVO apply = baseMapper.getApply(mutualId,companyId);

        return JsonResult.ok().setData(apply);
    }

    @Override
    public Integer allHists(HashMap<String, String> createTimeMap) {
        return baseMapper.allHists(createTimeMap);
    }

    private void applyNum(PubliclyMutualApplyVO applyNum){
        applyNum.setMayApplyNum(applyNum.getApplyAllNum() - applyNum.getAuditApproveNum());
    }

}
